package com.logictech.activity.system.service.impl;

import com.logictech.activity.system.dao.SystemAuthDao;
import com.logictech.activity.system.entity.Auth;
import com.logictech.activity.system.entity.SystemAuth;
import com.logictech.activity.system.entity.Menu;
import com.logictech.activity.system.entity.MenuFunction;
import com.logictech.activity.system.service.SystemAuthService;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import javax.annotation.Resource;
import org.jeecgframework.p3.core.utils.common.PageList;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.utils.common.Pagenation;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("systemAuthService")
public class SystemAuthServiceImpl implements SystemAuthService {
	@Resource
	private SystemAuthDao systemAuthDao;

	public void doAdd(SystemAuth systemAuth) {
		this.systemAuthDao.add(systemAuth);
	}

	public void doEdit(SystemAuth systemAuth) {
		this.systemAuthDao.update(systemAuth);
	}

	public void doDelete(Long id) {
		this.systemAuthDao.delete(id);
	}

	public SystemAuth queryById(Long id) {
		SystemAuth systemAuth = (SystemAuth) this.systemAuthDao.get(id);
		return systemAuth;
	}

	public PageList<SystemAuth> queryPageList(PageQuery<SystemAuth> pageQuery) {
		PageList<SystemAuth> result = new PageList();
		Integer itemCount = this.systemAuthDao.count(pageQuery);
		List<SystemAuth> list = this.systemAuthDao.queryPageList(pageQuery, itemCount);
		Pagenation pagenation = new Pagenation(pageQuery.getPageNo(), itemCount.intValue(), pageQuery.getPageSize());
		result.setPagenation(pagenation);
		result.setValues(list);
		return result;
	}

	public List<MenuFunction> queryMenuAndFuncAuth() {
		return this.systemAuthDao.queryMenuAndFuncAuth();
	}

	public List<MenuFunction> queryMenuAndFuncAuthByRoleId(String roleId) {
		return this.systemAuthDao.queryMenuAndFuncAuthByRoleId(roleId);
	}

	public Menu queryMenuByAuthId(String authId) {
		return this.systemAuthDao.queryMenuByAuthId(authId);
	}

	@Transactional(rollbackFor = { Exception.class })
	public void modifyOperateRoleAuthRel(String roleId, List<String> authIds) {
		this.systemAuthDao.deleteRoleAuthRels(roleId);
		if ((authIds != null) && (authIds.size() > 0)) {
			for (String authId : authIds) {
				this.systemAuthDao.insertRoleAuthRels(roleId, authId);
			}
		}
	}

	public LinkedHashMap<Menu, ArrayList<Menu>> getSubMenuTree(String userId, String parentAuthId) {
		List<Menu> allSubMenuList = getAllSubMenuList(userId, parentAuthId, new ArrayList());

		LinkedHashMap<Menu, ArrayList<Menu>> result = new LinkedHashMap();

		for (Menu menu : allSubMenuList) {
			if (isParentMenu(menu, allSubMenuList).booleanValue()) {
				ArrayList<Menu> subMenuList = getSubMenuList(allSubMenuList, menu.getAuthId());
				result.put(menu, subMenuList);
			} else if (!isSonMenu(menu, allSubMenuList).booleanValue()) {
				result.put(menu, null);
			}
		}

		return result;
	}

	private List<Menu> getAllSubMenuList(String userId, String parentAuthId, List<Menu> allSubMenu) {
		List<Menu> curSubMenu = this.systemAuthDao.queryMenuByUserIdAndParentAuthId(userId, parentAuthId);

		if (curSubMenu.size() == 0)
			return allSubMenu;
		for (Menu menu : curSubMenu) {
			allSubMenu.add(menu);
			int allNum = allSubMenu.size();
			allSubMenu = getAllSubMenuList(userId, menu.getAuthId(), allSubMenu);
			int tmpNum = allSubMenu.size();

			if (allNum == tmpNum) {
			}
		}

		return allSubMenu;
	}

	private Boolean isParentMenu(Menu curMenu, List<Menu> subMenuList) {
		for (Menu menu : subMenuList) {
			if (curMenu.getAuthId().equals(menu.getParentAuthId())) {
				return Boolean.valueOf(true);
			}
		}
		return Boolean.valueOf(false);
	}

	private Boolean isSonMenu(Menu curMenu, List<Menu> subMenuList) {
		for (Menu menu : subMenuList) {
			if (menu.getAuthId().equals(curMenu.getParentAuthId())) {
				return Boolean.valueOf(true);
			}
		}

		return Boolean.valueOf(false);
	}

	private ArrayList<Menu> getSubMenuList(List<Menu> subMenuList, String parentId) {
		ArrayList<Menu> result = new ArrayList();
		for (Menu menu : subMenuList) {
			if (parentId.equals(menu.getParentAuthId())) {
				result.add(menu);
			}
		}
		return result;
	}

	public List<Auth> queryAuthByUserId(String userId) {
		return this.systemAuthDao.queryAuthByUserId(userId);
	}

	public List<Auth> queryAuthByAuthContr(String authContr) {
		return this.systemAuthDao.queryAuthByAuthContr(authContr);
	}

	public List<Auth> queryAuthByUserIdAndAuthContr(String userId, String authContr) {
		return this.systemAuthDao.queryAuthByUserIdAndAuthContr(userId, authContr);
	}
}
